package com.marvin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marvin.core.Blog;
import com.marvin.core.BlogCategory;
import com.marvin.service.IBlogCategoryService;
import com.marvin.service.IBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Optional;

/**
 * @author yinyin
 * @create 2021/11/15 下午 3:42
 */
@Controller
public class BlogController {
    @Autowired
    private IBlogService blogService;
    @Autowired
    private IBlogCategoryService blogCategoryService;
    //查询博客列表
    @GetMapping("api/blogList")
    @ResponseBody
    public Page<Blog> findBlogs(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                @RequestParam(required = false,defaultValue = "0") Integer categoryId){
        Page page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper();
       //符合条件SQL语句再拼接categoryId
        queryWrapper.eq(categoryId!=null && !categoryId.equals(0),Blog::getCategoryId,categoryId)
                //查询已发布的
                    .eq(Blog::getStatus,1)
                    .eq(Blog::getIsDelete,0)
                    .orderByDesc(Blog::getCreateTime);

        return blogService.page(page,queryWrapper);
    }
    //查询博客分类
    @GetMapping("api/category")
    @ResponseBody
    public List<BlogCategory> findBlogCategory(){
        return blogCategoryService.findBlogCategory();
    }

    //新增页面
    @GetMapping("addBlog")
    public String addBlog(){
        //ModelAndView modelAndView = new ModelAndView();
        //modelAndView.setViewName("blog/add");
        return "blog/add";
    }
    //跳转到编辑页面
    @GetMapping("blog/edit/{id}")
    public ModelAndView toEdit(@PathVariable("id") Integer id){
       ModelAndView modelAndView=new ModelAndView();
       modelAndView.addObject("blogId",id);
       modelAndView.setViewName("blog/add");
       return modelAndView;
    }
    //根据id查询文章信息
    @GetMapping("/api/blog/get/{blogId}")
    @ResponseBody
    public Blog getBlogById(@PathVariable("blogId") Integer id){
        Optional.ofNullable(id).orElseThrow(()-> new RuntimeException("文章找不到"));
        Blog blog = blogService.getById(id);
        Optional.ofNullable(blog).orElseThrow(()-> new RuntimeException("文章找不到"));
        return  blog;
    }

    //保存博客
    @PostMapping("/saveBlog")
    @ResponseBody
    public void saveBlog(@RequestBody Blog blog){
         blog.setStatus(1);
         blog.setIsDelete(0);
         blog.setUserId(1);
         boolean b = blogService.saveOrUpdate(blog);

    }
    //获取博客详情
    @GetMapping("editBlog/{id}")
    @ResponseBody
    public Blog getBlog(@PathVariable Integer id){
        return blogService.getById(id);
    }


}
